<?php
include '../template.php';
include '../dbconfig/config.php';
include '../dbconfig/opendb.php';
include '../util/dbutil.php';
getTopWithJS("..");

	function check_email_address($email) {
		// First, we check that there's one @ symbol, and that the lengths are right
		if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
			// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
			return false;
		}
		// Split it into sections to make life easier
		$email_array = explode("@", $email);
		$local_array = explode(".", $email_array[0]);
		for ($i = 0; $i < sizeof($local_array); $i++) {
			if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
				return false;
			}
		}
		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
			$domain_array = explode(".", $email_array[1]);
			if (sizeof($domain_array) < 2) {
				return false; // Not enough parts to domain
			}
			for ($i = 0; $i < sizeof($domain_array); $i++) {
				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
					return false;
				}
			}
		}
		return true;
	}
	
	$good = false;
	$subannounce = true; // subscribe by default
        if (isset($_POST['skip'])) {
		$ip = getRealIpAddr();
		$query = "INSERT INTO download(name,ip,date) VALUES ('_skipped','$ip',NOW());";
                $good = true;
        } elseif(isset($_POST['submit'])) {
		$name = trim($_POST['name']);
		$org = trim($_POST['org']);
		$email = trim($_POST['email']);
		$ip = getRealIpAddr();
                $subannounce = isset($_POST['subannounce']);
		$good = true;
		if (strlen($name)==0) {
			$good = false;
			$err_name = "&nbsp;&nbsp;Please fill in your name.";
		}
		if (strlen($org)==0) {
			$good = false;
			$err_org = "&nbsp;&nbsp;Please fill in your organization.";
		}
		if (!check_email_address($email)) {
			$good = false;
			$err_email = "&nbsp;&nbsp;Please fill in a valid email address.";
		}

                if ($subannounce)
                        $query = "INSERT INTO download(name,organization,email,ip,maillist,date) VALUES ('$name','$org','$email','$ip',1,NOW());";
                else
                        $query = "INSERT INTO download(name,organization,email,ip,date) VALUES ('$name','$org','$email','$ip',NOW());";

	}
	if ($good) {
		//input information to database
		mysql_query($query);
?>

&nbsp;<br />
<h2><u>Download P<sup>3</sup>DB datasets in tab-delimited format:</u></h2>
<table border="0" cellspacing="0" cellpadding="0">
		<form method="get" action="download.php" target="_blank">
			<tr><td>Organism:</td>
				<td><select name="org" size="1">
					<option value="0" selected>All</option>
                                        <?php
                                                getOrganismSelect();
                                        ?>
					</select>
				</td>
			</tr>

                        <tr><td>&nbsp;</td></tr>
			<tr><td>Research group or reference:</td>
				<td><select name="ref" size="1">
					<option value="0" selected>All</option>
                                        <?php
                                                getDataSourceSelect();
                                        ?>
					</select>
				</td>
			</tr>

			<tr><td>&nbsp;</td></tr>

			<tr><td>File type:</td>
				<td><select name="type" size="1" id="type">
                                        <option value="pro">Phosphoprotein report</option>
                                        <option value="site">Phosphosite report</option>
                                        <option value="nrpep">Non-redundant phosphopeptide report</option>
                                        <option value="spec">Redundant phosphopeptide report</option>
                                        </select>
                                </td>
			</tr>

			<tr><td>&nbsp;</td></tr>

			<tr><td></td><td align="right">
                                <input name="download" type="submit" id="download" value="download"></td>
			<tr><td>&nbsp;</td></tr>
		</form>
		<tr><td colspan="3"><hr/></td></tr>

		<!--tr><td width="200">&nbsp;</td><td width="250">&nbsp;</td></tr-->

    </table>
	
<?php
		include '../dbconfig/closedb.php';
        } else {
?>

        &nbsp;<br />
        &nbsp;<br />
	<table border="0" cellspacing="0" cellpadding="0">
		<form method="post" action="./">

			<tr><td colspan="3"><b>We would like to collect information to understand the
                        P<sup>3</sup>DB user base. We will not use the information for other purposes.</b></td></tr>
			<tr><td>&nbsp;</td></tr>
			
			<tr><td align="right">Name:</td>
				<td><input name="name" size="40" type="text" id="name" value="<?php echo $name ?>"></td>
				<td><font color="red" size="1"><?php echo $err_name ?></font></td>
			</tr>
			
			<tr><td>&nbsp;</td></tr>
			
			<tr><td align="right">Organization:</td>
				<td><input name="org" size="40" type="text" id="org" value="<?php echo $org ?>"></td>
				<td><font color="red" size="1"><?php echo $err_org ?></font></td>
			</tr>
				
			<tr><td>&nbsp;</td></tr>
			
			<tr><td align="right">Email:</td>
				<td><input name="email" size="40" type="text" id="downloademail" value="<?php echo $email ?>"></td>
				<td><font color="red" size="1"><?php echo $err_email ?></font></td>
			</tr>

			<tr><td>&nbsp;</td></tr>

			<tr><td>&nbsp;</td>
				<td><input name="subannounce" type="checkbox" id="subannounce"<?php if ($subannounce) echo 'checked' ?>/>
                                    Subscribe me to the low-traffic announcement mailing list to keep updated on P<sup>3</sup>DB, such as future releases.<td>
			</tr>
				
			<tr><td>&nbsp;</td></tr>
			
			<tr><td></td><td align="right"><input name="submit" type="submit" id="downloadsubmit" value="Submit"> &nbsp;&nbsp;
                                <input name="skip" type="submit" id="skip" value="Skip"></td></tr>
			<tr><td>&nbsp;</td></tr>
		</form>

		
		<tr><td width="100">&nbsp;</td><td width="200">&nbsp;</td><td width="400">&nbsp;</td></tr>
	
    </table>	
<?php getBottom(); ?>
<?php } ?>




